package com.laowang.controller;

import com.laowang.convert.OrderForm2OrderDTO;
import com.laowang.dataoject.Demo;
import com.laowang.dto.OrderDTO;
import com.laowang.enums.ResultEnum;
import com.laowang.exception.SellException;
import com.laowang.form.OrderForm;
import com.laowang.sercice.BuyerService;
import com.laowang.sercice.OrderService;
import com.laowang.utils.ResultVoUtil;
import com.laowang.vo.ResultVo;
import lombok.Value;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import net.bytebuddy.implementation.bytecode.assign.TypeCasting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/1/15.
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;
    //创建订单
    //参数验证
    @PostMapping("/create")
    public ResultVo<Map<String,String>> create(@Valid OrderForm orderForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("[创建订单] 参数不正确, orderForm={}",orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode()
                    ,bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO = OrderForm2OrderDTO.convert(orderForm);
        if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
             log.error("[创建订单] 购物车不能为空");
             throw new SellException(ResultEnum.ORDER_CARSTACK_NOTNULL);

        }
        OrderDTO createResult = orderService.create(orderDTO);
        Map<String,String> map= new HashMap<>();
        map.put("orderId",createResult.getOrderId());

        return ResultVoUtil.success(map);
    }
    //订单列表
    @GetMapping("/list")
    public ResultVo<List<OrderDTO>> list(@RequestParam("openId") String openId,
                                         @RequestParam(value = "page" ,defaultValue = "0") Integer page ,
                                         @RequestParam(value = "size",defaultValue = "10") Integer size){
        if(StringUtils.isEmpty(openId)){
            log.error("查询时openId为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest(page,size);
        Page<OrderDTO> orderDTOPage = orderService.findList(openId, pageRequest);



        return ResultVoUtil.success(orderDTOPage.getContent());
    }
    //订单详情
     @GetMapping("/detail")
     public ResultVo<OrderDTO>  orderDetail(@RequestParam("openid") String openid
                                             ,@RequestParam("orderId") String orderId){
//         //todo 不安全做法，需要改进
//         buyerService.findOrderOne(openid,orderId);
//
//        OrderDTO orderDTO = orderService.findById(orderId);
//         return ResultVoUtil.success(orderDTO);
         OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId);
         return ResultVoUtil.success(orderDTO);
     }

    //取消订单
    @PostMapping("/cancel")
    public ResultVo<OrderDTO> cancel(@RequestParam("openid") String openid
                                      ,@RequestParam("orderId") String orderId){
//        //todo 不安全
//        OrderDTO orderDTO = orderService.findById(orderId);
//        orderService.cancel(orderDTO);
//        return ResultVoUtil.success();
        buyerService.cancel(openid,orderId);
        return ResultVoUtil.success();
    }
}
